namespace Warehouse

open System.Windows.Data
open Warehouse.Controls

open Wpf.Mvc

[<AbstractClass>]
type UPCSearchModel() = 
    inherit Model()

    abstract ProductSearch : ProductSearchModel with get, set
    abstract SelectedUPC : UpcModel with get, set    

type UPCSearchView() as this = 
    inherit StaticView<UPCSearchModel, UPCSearchWindow>()

    do 
        this.OKButton <- this.Control.OK
    
    override this.SetBindings model =
        Binding.FromExpression 
            <@ 
                this.Control.UPCs.ItemsSource <- model.ProductSearch.SelectedProduct.UPCs
                this.Control.UPCs.SelectedItem <- model.SelectedUPC
            @>

        Binding.FromExpression 
            <@ 
                this.Control.OK.IsEnabled <- isNotNull model.SelectedUPC 
            @>

type UPCSearchController(view, ?repository) = 
    inherit StaticViewController<UPCSearchModel>(view)

    let repository = defaultArg repository (new DataModel.Repository())

    override this.InitModel model = 
        model.ProductSearch <- Model.Create()

    static member Create(?repository) =
        let view = UPCSearchView()
        UPCSearchController(view, ?repository = repository)
            .Compose(
                ProductSearchChildController(?repository = repository), 
                ProductSearchPartialView(view.Control.ProductSearch), 
                fun m -> m.ProductSearch)
               
